<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="600px"
  >
    <form-create
      v-model="fApi"
      :rule="tableSeting"
      :option="option"
    ></form-create>
    <div slot="footer">
      <ElButton aligen="center" plain @click="reset">重置</ElButton>
      <el-button @click="visible = false">取 消</el-button>
      <ElButton type="primary" plain @click="submit">提交</ElButton>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    },
    tableSeting: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    // 表单名称验证

    return {
      visible: false,
      // 实例对象
      fApi: {},
      // 表单生成规则

      // 组件参数配置
      option: {
        // 表单提交事件
        submitBtn: false,
        onSubmit: function(formData) {
          alert(JSON.stringify(formData))
        }
      }
    }
  },
  methods: {
    submit() {
      this.fApi.submit((formData, $f) => {
        //   alert(JSON.stringify(formData))
        this.$emit('success', formData)
      })
    },
    reset() {
      this.fApi.resetFields()
    },
    // 处理Dialog关闭事件
    handleDialogClose() {
      console.log(this.form)
      this.$emit('update:show')
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            /*            FormDesignService.updateForm(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success')
            }) */
            let formList = JSON.parse(localStorage.getItem('formList'))
            let index = formList.findIndex(el => el.id === this.form.id)
            console.log(formList)
            console.log(this.form)
            console.log(index)
            formList[index] = JSON.parse(JSON.stringify(this.form))
            localStorage.setItem('formList', JSON.stringify(formList))
            this.visible = false
            this.$emit('success')
          } else {
            /*  FormDesignService.insertForm(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success')
            }) */
            console.log(22222222)
            this.form.id = new Date().getTime()
            this.form.createTime = new Date().toDateString()
            let formList = JSON.parse(localStorage.getItem('formList'))
            formList = formList === null ? [] : formList
            formList.push(JSON.parse(JSON.stringify(this.form)))
            localStorage.setItem('formList', JSON.stringify(formList))
            this.visible = false
            this.$emit('success')
          }
        }
      })
    }
  },
  watch: {
    show(value) {
      this.visible = value
    },
    tableSeting: {
      handler: function(newValue, oldValue) {
        newValue.forEach(e => {
          if (e.type === 'upload') {
            console.log(newValue)
            console.log(this.fApi)
            if (this.fApi.updateRule) {
              let props = JSON.parse(JSON.stringify(e.props))
              let field = e.field
              this.fApi.updateRule(
                field,
                {
                  props: {
                    ...props,
                    onSuccess: function(a, b) {
                      console.log(a)
                      console.log(b)
                      // file.url = res.data.filePath;
                    },
                    onError: function(a, b) {
                      console.log(a)
                      console.log(b)
                    }
                  }
                },
                true
              )
            }
          }
        })
      },
      deep: false
    },
    data(value) {
      this.form = value || {
        id: '',
        formName: '',
        description: ''
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
</style>
